package com.njrsrc.android.app;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.njrsrc.R;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.android.tasks.NewsDetailTask;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.url.RequestUrlProvider;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

public class NewsDetailActivity extends BaseActivity implements OnClickListener {
	private TitleBar mBar;
	private WebView webView;
	private ProgressDialog progressDialog;
	
	private String newsId;
	private String requestUrl;
	private UserInfo userInfo;
	
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MsgID.NEWS_DETAIL_QUERY_ERROR:
				Helpers.closeProgressDialog(progressDialog);
				Toast.makeText(NewsDetailActivity.this, 
						getResources().getString(R.string.connectError), 
						Toast.LENGTH_SHORT).show();
				break;
			case MsgID.NEWS_DETAIL_QUERY_FAIL:
				Helpers.closeProgressDialog(progressDialog);
				break;
			case MsgID.NEWS_DETAIL_QUERY_SUCCESS:
				Helpers.closeProgressDialog(progressDialog);
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_detail);
		mBar = (TitleBar) findViewById(R.id.activity_title_bar);
		mBar.back.setOnClickListener(this);
		
		webView = (WebView) findViewById(R.id.webView);
		webView.setInitialScale(220);
		WebSettings s = webView.getSettings();
		s.setBuiltInZoomControls(true);
		s.setSupportZoom(true);
		s.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		
        s.setUseWideViewPort(false);
        s.setLoadsImagesAutomatically(true);
        s.setJavaScriptEnabled(true);
        s.setPluginsEnabled(true);
        s.setJavaScriptCanOpenWindowsAutomatically(true);
        s.setDefaultTextEncodingName("utf-8");
        s.setNavDump(true);
        s.setDefaultZoom(ZoomDensity.MEDIUM);
        s.setLightTouchEnabled(true);
        s.setSaveFormData(false);
        s.setSavePassword(false);
        s.setLoadWithOverviewMode(true);

        // WebView inside Browser doesn't want initial focus to be set.
        s.setNeedInitialFocus(false);
        // Browser supports multiple windows
        s.setSupportMultipleWindows(false);

        // HTML5 API flags
        s.setAppCacheEnabled(true);
        s.setDatabaseEnabled(true);
        s.setDomStorageEnabled(true);
		
		Bundle data = getIntent().getExtras();
		newsId = data.getString("newsId");
		
		webView.setWebViewClient(new WebViewClient(){
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				Helpers.closeProgressDialog(progressDialog);
			}

			@Override
			public void onPageStarted(WebView view, String url,
					Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
			}
		});
		
		progressDialog = Helpers.showProgressDialog(NewsDetailActivity.this, R.string.loading);
		initData();
	}
	
	private void initData()
	{
		List<String> params = new ArrayList<String>();
		params.add(AppApplication.mToken);
		params.add(AppApplication.mUserID);
		params.add(newsId);
		requestUrl = RequestUrlProvider.NEWS_SHOW;
		String url = Helpers.combinaStr(requestUrl, params);
		userInfo = new UserInfo();
		Object[] args = new Object[4];
		args[0] = mHandler;
		args[1] = url;
		args[2] = userInfo;
		args[3] = webView;
		new NewsDetailTask().execute(args);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tb_ibtn_back:
			finish();
			break;
		default:
			break;
		}
	}
	
	

}
